package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {

    @Autowired
    private EmpService empService;

    @PostMapping("login")
    public Result login(@RequestBody Emp emp){
        log.info("员工登录，{}",emp);
        Emp res=empService.login(emp);

        Map<String, Object> claims=new HashMap<>();
        //登录成功
        if(res!=null){
            claims.put("id",res.getId());
            claims.put("name",res.getName());
            claims.put("username",res.getUsername());

            String jwt = JwtUtils.generateJwt(claims);
            return Result.success(jwt);

        }
        else{   //登录失败
            return Result.error("用户名或密码错误");
        }
    }

}
